<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\response\Json;

/**
 * @mixin \think\Model
 */
class CouponProduct extends Base
{

    protected $name = 'coupon_product';

    public static function couponProductAdd($coupon_id,$product_ids)
    {   parent::startTrans();
        try {
            parent::where('coupon_id',$coupon_id)->update(['is_del'=>1]);
            foreach($product_ids as $k=>$v){
                $find= parent::where(['coupon_id'=>$coupon_id,'product_id'=>$v])->find();
                if(empty($find)){
                    $data['coupon_id']=$coupon_id;
                    $data['product_id']=$v;
                    parent::create($data);
                }else{
                    $find->is_del=0;
                    $find->save();
                }
            }
            parent::commit();
            return ['code' => 1, 'msg' =>'操作成功！'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

}
